﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.OleDb;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using DM_Article.Model;
using DM_Article.Bll;

public partial class admin_admin_adminadd : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            if (Session["username"] == null)
            {
                Response.Write("<script language='javascript'>alert('非法操作，请先登录！');location.href = 'admin_login.aspx';</script>");
            }
            else if (!checkUserType(Session["username"].ToString()))
            {
                Response.Write("<script language='javascript'>alert('权限不足！');location.href = 'admin_server.aspx';</script>");
            }
        }

    }

    //权限管理
    private bool checkUserType(string username)
    {
        using (OleDbConnection cn = new OleDbConnection(ConfigurationManager.ConnectionStrings["ConnectionStringAccess"].ConnectionString))
        {
            cn.Open();
            admin adn = adminbll.getAdminDetailByName(cn, username);
            return (adn.usertype == "0");
        }
    }

    protected void btn_adminadd_Click(object sender, EventArgs e)
    {
        using (OleDbConnection cn = new OleDbConnection(ConfigurationManager.ConnectionStrings["ConnectionStringAccess"].ConnectionString))
        {
            cn.Open();
            if (checkAdmin())
            {
                if (adminbll.add(cn, getAdmin()) > 0)
                {
                    Response.Write("<script language='javascript'>alert('添加成功！');location.href = 'admin_adminlist.aspx';</script>");
                }
                else
                {
                    Response.Write("<script language='javascript'>alert('添加失败！');location.href = 'admin_articleadd.aspx';</script>");
                }
            }
        }
    }

    private admin getAdmin()
    {
        admin adn = new admin();
        adn.username = this.txtadminName.Text.Trim();
        adn.password = this.txtpassword.Text.Trim();
        adn.usertype = this.ddlUserType.SelectedValue.Trim();
        return adn;
    }

    private bool checkAdmin()
    {
        if (this.txtadminName.Text == "")
        {
            Response.Write("<script language='javascript'>alert('[用户名]不能为空！');</script>");
            this.txtadminName.Focus();
            return false;
        }
        if (this.txtpassword.Text == "")
        {
            Response.Write("<script language='javascript'>alert('[密码]不能为空！');</script>");
            this.txtadminName.Focus();
            return false;
        }
        if (this.txtconfirmPassword.Text == "")
        {
            Response.Write("<script language='javascript'>alert('[确认密码]不能为空！');</script>");
            this.txtadminName.Focus();
            return false;
        }
        if (this.txtconfirmPassword.Text != this.txtpassword.Text)
        {
            Response.Write("<script language='javascript'>alert('输入[密码]与[确认密码]不同！');</script>");
            this.txtadminName.Focus();
            return false;
        }
        return true;
    }

    protected void btn_back_Click(object sender, EventArgs e)
    {
        Response.Redirect("admin_server.aspx");
    }
}
